package org.apache.james.mime4j.decoder;

import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Base64InputStream extends InputStream
{
  private static byte[] TRANSLATION;
  static Class class$org$apache$james$mime4j$decoder$Base64InputStream;
  private static Log log;
  private final ByteQueue byteq = new ByteQueue(3);
  private boolean closed;
  private boolean done = false;
  private final InputStream s;

  static
  {
    Class localClass;
    if (class$org$apache$james$mime4j$decoder$Base64InputStream == null)
    {
      localClass = class$("org.apache.james.mime4j.decoder.Base64InputStream");
      class$org$apache$james$mime4j$decoder$Base64InputStream = localClass;
    }
    while (true)
    {
      log = LogFactory.getLog(localClass);
      byte[] arrayOfByte = new byte[256];
      arrayOfByte[0] = -1;
      arrayOfByte[1] = -1;
      arrayOfByte[2] = -1;
      arrayOfByte[3] = -1;
      arrayOfByte[4] = -1;
      arrayOfByte[5] = -1;
      arrayOfByte[6] = -1;
      arrayOfByte[7] = -1;
      arrayOfByte[8] = -1;
      arrayOfByte[9] = -1;
      arrayOfByte[10] = -1;
      arrayOfByte[11] = -1;
      arrayOfByte[12] = -1;
      arrayOfByte[13] = -1;
      arrayOfByte[14] = -1;
      arrayOfByte[15] = -1;
      arrayOfByte[16] = -1;
      arrayOfByte[17] = -1;
      arrayOfByte[18] = -1;
      arrayOfByte[19] = -1;
      arrayOfByte[20] = -1;
      arrayOfByte[21] = -1;
      arrayOfByte[22] = -1;
      arrayOfByte[23] = -1;
      arrayOfByte[24] = -1;
      arrayOfByte[25] = -1;
      arrayOfByte[26] = -1;
      arrayOfByte[27] = -1;
      arrayOfByte[28] = -1;
      arrayOfByte[29] = -1;
      arrayOfByte[30] = -1;
      arrayOfByte[31] = -1;
      arrayOfByte[32] = -1;
      arrayOfByte[33] = -1;
      arrayOfByte[34] = -1;
      arrayOfByte[35] = -1;
      arrayOfByte[36] = -1;
      arrayOfByte[37] = -1;
      arrayOfByte[38] = -1;
      arrayOfByte[39] = -1;
      arrayOfByte[40] = -1;
      arrayOfByte[41] = -1;
      arrayOfByte[42] = -1;
      arrayOfByte[43] = 62;
      arrayOfByte[44] = -1;
      arrayOfByte[45] = -1;
      arrayOfByte[46] = -1;
      arrayOfByte[47] = 63;
      arrayOfByte[48] = 52;
      arrayOfByte[49] = 53;
      arrayOfByte[50] = 54;
      arrayOfByte[51] = 55;
      arrayOfByte[52] = 56;
      arrayOfByte[53] = 57;
      arrayOfByte[54] = 58;
      arrayOfByte[55] = 59;
      arrayOfByte[56] = 60;
      arrayOfByte[57] = 61;
      arrayOfByte[58] = -1;
      arrayOfByte[59] = -1;
      arrayOfByte[60] = -1;
      arrayOfByte[61] = -1;
      arrayOfByte[62] = -1;
      arrayOfByte[63] = -1;
      arrayOfByte[64] = -1;
      arrayOfByte[65] = 0;
      arrayOfByte[66] = 1;
      arrayOfByte[67] = 2;
      arrayOfByte[68] = 3;
      arrayOfByte[69] = 4;
      arrayOfByte[70] = 5;
      arrayOfByte[71] = 6;
      arrayOfByte[72] = 7;
      arrayOfByte[73] = 8;
      arrayOfByte[74] = 9;
      arrayOfByte[75] = 10;
      arrayOfByte[76] = 11;
      arrayOfByte[77] = 12;
      arrayOfByte[78] = 13;
      arrayOfByte[79] = 14;
      arrayOfByte[80] = 15;
      arrayOfByte[81] = 16;
      arrayOfByte[82] = 17;
      arrayOfByte[83] = 18;
      arrayOfByte[84] = 19;
      arrayOfByte[85] = 20;
      arrayOfByte[86] = 21;
      arrayOfByte[87] = 22;
      arrayOfByte[88] = 23;
      arrayOfByte[89] = 24;
      arrayOfByte[90] = 25;
      arrayOfByte[91] = -1;
      arrayOfByte[92] = -1;
      arrayOfByte[93] = -1;
      arrayOfByte[94] = -1;
      arrayOfByte[95] = -1;
      arrayOfByte[96] = -1;
      arrayOfByte[97] = 26;
      arrayOfByte[98] = 27;
      arrayOfByte[99] = 28;
      arrayOfByte[100] = 29;
      arrayOfByte[101] = 30;
      arrayOfByte[102] = 31;
      arrayOfByte[103] = 32;
      arrayOfByte[104] = 33;
      arrayOfByte[105] = 34;
      arrayOfByte[106] = 35;
      arrayOfByte[107] = 36;
      arrayOfByte[108] = 37;
      arrayOfByte[109] = 38;
      arrayOfByte[110] = 39;
      arrayOfByte[111] = 40;
      arrayOfByte[112] = 41;
      arrayOfByte[113] = 42;
      arrayOfByte[114] = 43;
      arrayOfByte[115] = 44;
      arrayOfByte[116] = 45;
      arrayOfByte[117] = 46;
      arrayOfByte[118] = 47;
      arrayOfByte[119] = 48;
      arrayOfByte[120] = 49;
      arrayOfByte[121] = 50;
      arrayOfByte[122] = 51;
      arrayOfByte[123] = -1;
      arrayOfByte[124] = -1;
      arrayOfByte[125] = -1;
      arrayOfByte[126] = -1;
      arrayOfByte[127] = -1;
      arrayOfByte[128] = -1;
      arrayOfByte[129] = -1;
      arrayOfByte[130] = -1;
      arrayOfByte[131] = -1;
      arrayOfByte[132] = -1;
      arrayOfByte[133] = -1;
      arrayOfByte[134] = -1;
      arrayOfByte[135] = -1;
      arrayOfByte[136] = -1;
      arrayOfByte[137] = -1;
      arrayOfByte[138] = -1;
      arrayOfByte[139] = -1;
      arrayOfByte[140] = -1;
      arrayOfByte[141] = -1;
      arrayOfByte[142] = -1;
      arrayOfByte[143] = -1;
      arrayOfByte[144] = -1;
      arrayOfByte[145] = -1;
      arrayOfByte[146] = -1;
      arrayOfByte[147] = -1;
      arrayOfByte[148] = -1;
      arrayOfByte[149] = -1;
      arrayOfByte[150] = -1;
      arrayOfByte[151] = -1;
      arrayOfByte[152] = -1;
      arrayOfByte[153] = -1;
      arrayOfByte[154] = -1;
      arrayOfByte[155] = -1;
      arrayOfByte[156] = -1;
      arrayOfByte[157] = -1;
      arrayOfByte[158] = -1;
      arrayOfByte[159] = -1;
      arrayOfByte[160] = -1;
      arrayOfByte[161] = -1;
      arrayOfByte[162] = -1;
      arrayOfByte[163] = -1;
      arrayOfByte[164] = -1;
      arrayOfByte[165] = -1;
      arrayOfByte[166] = -1;
      arrayOfByte[167] = -1;
      arrayOfByte[168] = -1;
      arrayOfByte[169] = -1;
      arrayOfByte[170] = -1;
      arrayOfByte[171] = -1;
      arrayOfByte[172] = -1;
      arrayOfByte[173] = -1;
      arrayOfByte[174] = -1;
      arrayOfByte[175] = -1;
      arrayOfByte[176] = -1;
      arrayOfByte[177] = -1;
      arrayOfByte[178] = -1;
      arrayOfByte[179] = -1;
      arrayOfByte[180] = -1;
      arrayOfByte[181] = -1;
      arrayOfByte[182] = -1;
      arrayOfByte[183] = -1;
      arrayOfByte[184] = -1;
      arrayOfByte[185] = -1;
      arrayOfByte[186] = -1;
      arrayOfByte[187] = -1;
      arrayOfByte[188] = -1;
      arrayOfByte[189] = -1;
      arrayOfByte[190] = -1;
      arrayOfByte[191] = -1;
      arrayOfByte[192] = -1;
      arrayOfByte[193] = -1;
      arrayOfByte[194] = -1;
      arrayOfByte[195] = -1;
      arrayOfByte[196] = -1;
      arrayOfByte[197] = -1;
      arrayOfByte[198] = -1;
      arrayOfByte[199] = -1;
      arrayOfByte[200] = -1;
      arrayOfByte[201] = -1;
      arrayOfByte[202] = -1;
      arrayOfByte[203] = -1;
      arrayOfByte[204] = -1;
      arrayOfByte[205] = -1;
      arrayOfByte[206] = -1;
      arrayOfByte[207] = -1;
      arrayOfByte[208] = -1;
      arrayOfByte[209] = -1;
      arrayOfByte[210] = -1;
      arrayOfByte[211] = -1;
      arrayOfByte[212] = -1;
      arrayOfByte[213] = -1;
      arrayOfByte[214] = -1;
      arrayOfByte[215] = -1;
      arrayOfByte[216] = -1;
      arrayOfByte[217] = -1;
      arrayOfByte[218] = -1;
      arrayOfByte[219] = -1;
      arrayOfByte[220] = -1;
      arrayOfByte[221] = -1;
      arrayOfByte[222] = -1;
      arrayOfByte[223] = -1;
      arrayOfByte[224] = -1;
      arrayOfByte[225] = -1;
      arrayOfByte[226] = -1;
      arrayOfByte[227] = -1;
      arrayOfByte[228] = -1;
      arrayOfByte[229] = -1;
      arrayOfByte[230] = -1;
      arrayOfByte[231] = -1;
      arrayOfByte[232] = -1;
      arrayOfByte[233] = -1;
      arrayOfByte[234] = -1;
      arrayOfByte[235] = -1;
      arrayOfByte[236] = -1;
      arrayOfByte[237] = -1;
      arrayOfByte[238] = -1;
      arrayOfByte[239] = -1;
      arrayOfByte[240] = -1;
      arrayOfByte[241] = -1;
      arrayOfByte[242] = -1;
      arrayOfByte[243] = -1;
      arrayOfByte[244] = -1;
      arrayOfByte[245] = -1;
      arrayOfByte[246] = -1;
      arrayOfByte[247] = -1;
      arrayOfByte[248] = -1;
      arrayOfByte[249] = -1;
      arrayOfByte[250] = -1;
      arrayOfByte[251] = -1;
      arrayOfByte[252] = -1;
      arrayOfByte[253] = -1;
      arrayOfByte[254] = -1;
      arrayOfByte[255] = -1;
      TRANSLATION = arrayOfByte;
      return;
      localClass = class$org$apache$james$mime4j$decoder$Base64InputStream;
    }
  }

  public Base64InputStream(InputStream paramInputStream)
  {
    this.closed = false;
    this.s = paramInputStream;
  }

  static Class class$(String paramString)
  {
    Class localClass;
    try
    {
      localClass = Class.forName(paramString);
      return localClass;
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      throw new NoClassDefFoundError().initCause(localClassNotFoundException);
    }
  }

  private void decodeAndEnqueue(byte[] paramArrayOfByte, int paramInt)
  {
    int i = 0x0 | paramArrayOfByte[0] << 18 | paramArrayOfByte[1] << 12 | paramArrayOfByte[2] << 6 | paramArrayOfByte[3];
    byte b1 = (byte)(i >>> 16);
    this.byteq.enqueue(b1);
    if (paramInt > 2)
    {
      byte b2 = (byte)(0xFF & i >>> 8);
      this.byteq.enqueue(b2);
      if (paramInt > 3)
      {
        byte b3 = (byte)(i & 0xFF);
        this.byteq.enqueue(b3);
      }
    }
  }

  private void fillBuffer()
    throws IOException
  {
    byte[] arrayOfByte = new byte[4];
    int i = 0;
    while (true)
    {
      int l;
      while (true)
      {
        int j;
        int k;
        do
        {
          if (this.done)
            break label84;
          j = this.s.read();
          switch (j)
          {
          default:
            k = TRANSLATION[j];
          case -1:
          case 61:
          }
        }
        while (k < 0);
        l = i + 1;
        arrayOfByte[i] = k;
        if (l != arrayOfByte.length)
          break;
        decodeAndEnqueue(arrayOfByte, l);
        while (true)
        {
          do
            label84: return;
          while (i <= 0);
          log.warn("Unexpected EOF in MIME parser, dropping " + i + " sextets");
        }
        decodeAndEnqueue(arrayOfByte, i);
        this.done = true;
      }
      i = l;
    }
  }

  public void close()
    throws IOException
  {
    this.closed = true;
  }

  public int read()
    throws IOException
  {
    int j;
    if (this.closed)
      throw new IOException("Base64InputStream has been closed");
    if (this.byteq.count() == 0)
    {
      fillBuffer();
      if (this.byteq.count() == 0)
        j = -1;
    }
    while (true)
    {
      int i;
      while (true)
      {
        return j;
        i = this.byteq.dequeue();
        if (i < 0)
          break;
        j = i;
      }
      j = i & 0xFF;
    }
  }
}